package com.taomeishu.mailcrazy.fetch;

import com.taomeishu.mailcrazy.checkers.Cn21MailExistChecker;
import com.taomeishu.mailcrazy.checkers.GmailMailExistChecker;
import com.taomeishu.mailcrazy.checkers.HotmailMailExistChecker;
import com.taomeishu.mailcrazy.checkers.Net263MailExistChecker;
import com.taomeishu.mailcrazy.checkers.NeteasyMailExistChecker;
import com.taomeishu.mailcrazy.checkers.SinaMailExistChecker;
import com.taomeishu.mailcrazy.checkers.SohuMailExistChecker;
import com.taomeishu.mailcrazy.checkers.TomMailExistChecker;
import com.taomeishu.mailcrazy.fetch.policy.DelayPolicy;
import com.taomeishu.mailcrazy.fetch.policy.RandomDelayPolicy;

public class FetchMailMain {

	private FetchMailService fetchMailService;
	
	private EmailManager emailManager;
	
	private CharseqNumberManger charseqNumberManger;
	
	private boolean start = false;

	public synchronized void main() {
		if (start) {
			return;
		}
		fetchMailService = new DefaultFetchMailService(emailManager, charseqNumberManger);
		fetchMailService.register("163.com", new NeteasyMailExistChecker(),createDelayPolicy());
		fetchMailService.register("tom.com", new TomMailExistChecker(), createDelayPolicy());
		fetchMailService.register("sohu.com", new SohuMailExistChecker(), createDelayPolicy());
		fetchMailService.register("sina.com", new SinaMailExistChecker(), createDelayPolicy());
		fetchMailService.register("qq.com", new SinaMailExistChecker(), createDelayPolicy());
		fetchMailService.register("263.net", new Net263MailExistChecker(), createDelayPolicy());
		fetchMailService.register("hotmail.com", new HotmailMailExistChecker(), createDelayPolicy());
		fetchMailService.register("21cn.com", new Cn21MailExistChecker(), createDelayPolicy());
		fetchMailService.register("gmail.com", new GmailMailExistChecker(), createDelayPolicy());
		
		fetchMailService.init();
		
		fetchMailService.start();
		
		start = true;
	}
	
	public void close() {
		fetchMailService.stop();
	}
	
	protected DelayPolicy createDelayPolicy() {
		return new RandomDelayPolicy(1, 2);
	}

	public void setEmailManager(EmailManager emailManager) {
		this.emailManager = emailManager;
	}

	public void setCharseqNumberManger(CharseqNumberManger charseqNumberManger) {
		this.charseqNumberManger = charseqNumberManger;
	}
	
}
